package cn.rocksolid.proxy.restful.agency;

import cn.rocksolid.sport.agency.entity.SmtpTemplateEntity;
import cn.rocksolid.sport.agency.service.SmtpSettingService;
import cn.rocksolid.sport.agency.service.SmtpTemplateService;
import cn.rocksolid.sport.common.error.RSE;
import cn.rocksolid.sport.common.http.R;
import cn.rocksolid.sport.common.http.utils.HttpUtils;
import cn.rocksolid.sport.common.utils.AssertUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("agency/smtp-template")
public class SmtpTemplateController {

  private final SmtpTemplateService smtpTemplateService;
  private final SmtpSettingService smtpSettingService;

  @Autowired
  public SmtpTemplateController(final SmtpTemplateService smtpTemplateService,
          final SmtpSettingService smtpSettingService) {
    this.smtpTemplateService = smtpTemplateService;
    this.smtpSettingService = smtpSettingService;
  }

  /**
   * pagination of agency SMTP templates
   */
  @PostMapping("/page")
  @RequiresPermissions("agency:smtp-template:info")
  public Object page(@RequestBody final JSONObject cond) {
    return smtpTemplateService.paging(HttpUtils.resolve(), cond);
  }

  /**
   * Get
   */
  @GetMapping("/{id}")
  @RequiresPermissions("agency:smtp-template:info")
  public Object get(@PathVariable("id") final String id) {
    SmtpTemplateEntity smtpTemplateEntity = smtpTemplateService.getById(id);
    if (null != smtpTemplateEntity && StringUtils.isNotBlank(smtpTemplateEntity.getSmtpSettingId())) {
      smtpTemplateEntity.setSmtpSetting(smtpSettingService.getById(smtpTemplateEntity.getSmtpSettingId()));
    }
    return smtpTemplateEntity;
  }

  /**
   * create
   */
  @PutMapping
  @RequiresPermissions("agency:smtp-template:create")
  public Object create(@RequestBody final SmtpTemplateEntity smtpTemplateEntity) {
    smtpTemplateValidator(smtpTemplateEntity);
    smtpTemplateService.save(smtpTemplateEntity);
    return R.ok();
  }

  /**
   * modify
   */
  @PostMapping
  @RequiresPermissions("agency:smtp-template:modify")
  public Object modify(@RequestBody final SmtpTemplateEntity smtpTemplateEntity) {
    smtpTemplateValidator(smtpTemplateEntity);
    smtpTemplateService.updateById(smtpTemplateEntity);
    return R.ok();
  }

  /**
   * remove
   */
  @DeleteMapping
  @RequiresPermissions("agency:smtp-template:remove")
  public Object remove(@RequestBody final List<String> ids) {
    smtpTemplateService.removeByIds(ids);
    return R.ok();
  }

  /**
   * validate agency smtpSetting
   */
  public void smtpTemplateValidator(SmtpTemplateEntity smtpTemplateEntity) {
    AssertUtils.isNotNull(smtpTemplateEntity, RSE.INVALID_PAYLOAD);
    AssertUtils.isName(smtpTemplateEntity.getTitle(), RSE.INVALID_AGSMTPT_NAME);
    AssertUtils.isNotNull(smtpTemplateEntity.getSmtpSettingId(), RSE.INVALID_AGSMTPT_SID);
    AssertUtils.isNotNull(smtpTemplateEntity.getTemplate(), RSE.INVALID_AGSMTPT_TL);
  }
}
